package engine.motionDetection.modelisation;

import java.util.Arrays;

import testAndSomeTries.motionDetection.Pixel;

/**
 * 
 * Class of an image represented by some Pixel
 * @author HINB
 *
 * @see Pixel
 */
public class ImagePix<T>
{
	
	private static final long serialVersionUID = 1;

	/* *********************************************************
	 * Attributes
	 * ******************************************************** */	
	/**
	 * Width of the image
	 */
	private int _width;
	
	/**
	 * Height of the image
	 */
	private int _height;
	
	/**
	 * Set of Pixel
	 */
	protected T[][] _matrix;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	/**
	 * Create an ImagePix with only one black Pixel
	 */
	@SuppressWarnings("unchecked")
	public ImagePix()
	{
		//System.out.println("eg.motDetec.model imagePix default");
		_width = 1;
		_height = 1;
		_matrix = (T[][]) new Object[1][1];
		_matrix[0][0] = null;

	}
	
	
	/**
	 * Create an ImagePix with the specified size and only black Pixel
	 * @param width width of the image
	 * @param height height of the image
	 */
	@SuppressWarnings("unchecked")
	public ImagePix(int width, int height)
	{
		//System.out.println("eg.motDetec.model imagePix width heigt");
		_width = width;
		_height = height;
		_matrix = (T[][]) new Object[width][height];
	}
	
	public ImagePix(int width, int height, T defaultValue){
		_width = width;
		_height = height;
		_matrix = (T[][]) new Object[width][height];
		for(int i = 0; i< width; i++){
			Arrays.fill(_matrix[i], defaultValue);
		}
	}
	
	/**
	 * Create an ImagePix from an initial ImagePix
	 * @param image original ImagePix
	 */
	@SuppressWarnings("unchecked")
	public ImagePix(ImagePix<T> image)
	{
		_width = image.getWidth();
		_height = image.getHeight();
		_matrix = (T[][]) new Object[_width][_height];
		
		
		for(int j=0; j<_height; j++)
		{
			for(int i=0; i<_width; i++)
			{
				_matrix[i][j] = image.getPix(i,j);			
			}
		}
	}
	
		
	/* *********************************************************
	 * Instance Methods
	 * *********************************************************
	 */
	/**
	 * Set the value of the specified Pixel
	 * @param x value on the x-axis of the Pixel
	 * @param y value on the y-axis of the Pixel
	 * @param pix value of the Pixel
	 */
	public void setPix(int x, int y, T elm)
	{
		if((x < _width) && (y < _height))
			_matrix[x][y] = elm;
	}
	
	/**
	 * Get the Pixel at a specified position
	 * @param x value on the x-axis of the Pixel
	 * @param y value on the y-axis of the Pixel
	 * @return the Pixel at the specified position
	 */
	public T getPix(int x, int y)
	{
		if((x < _width) && (y < _height)){
			return _matrix[x][y];
		}else{
			throw new IndexOutOfBoundsException();
		}
	}
	
	
	/**
	 * Get the ImagePix width
	 * @return the width of the ImagePix
	 */
	public int getWidth()
	{
		return _width;
	}
	
	
	/**
	 * Get the ImagePix height
	 * @return the height of the ImagePix
	 */
	public int getHeight()
	{
		return _height;
	}
}
